package edu.thu.studio.ormdesigner.editor.command;

import edu.thu.studio.ormdesigner.common.AbstractCommand;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNode;
import edu.thu.studio.ormdesigner.model.orm.table.Table;
import edu.thu.studio.ormdesigner.util.Utils;

public class CreateTableCommand extends AbstractCommand {
    
    private Diagram diagram;
    
    private Table table;
    
    private TableNode node;
    
    public CreateTableCommand(Diagram diagram, TableNode node) {
        this.diagram = diagram;
        this.table = new Table();
        table.checkAndInit();
        
        node.setRefId(table.getId());
        node.setDiagram(diagram);
        node.setRefTable(table);
        node.checkAndInit();
        this.node = node;
    }
    
    public CreateTableCommand(Diagram diagram, TableNode node, Table table) {
        this.diagram = diagram;
        this.table = table;
        table.checkAndInit();
        
        node.setRefId(table.getId());
        node.setDiagram(diagram);
        node.setRefTable(table);
        node.checkAndInit();
        this.node = node;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void doExecute() {
        Utils.getTableSet().addObject(table);
        this.diagram.getNodes().addObject(node);
        this.diagram.refreshChildren();
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void doUndo() {
        Utils.getTableSet().remove(table);
        this.diagram.getNodes().remove(node);
        this.diagram.refreshChildren();
    }
    
}
